﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;
using System.IO;

namespace KPIS.GERP.GIMS.APP.APP_UI.UserControl
{
    public partial class TIP_Bid_Confirm : System.Web.UI.UserControl
    {
        Common.Main cn = new Common.Main();
        public CallBackHandle RefreshParentDisplay;
        public CallBackHandleReturnInt GetBidSeq;
        public CallBackHandleAlert Alert;
        PurchaseRequestBLL bllPr = new PurchaseRequestBLL();
        IBLLPrePurchaseRequestItem bllPprItem = new PrePurchaseRequestItemBLL();
        List<string[]> ApproveControlName = new List<string[]>();
        List<string> DelButtonApproveName = new List<string>();
        List<string[]> KnowControlName = new List<string[]>();
        List<string> DelButtonKnowName = new List<string>();
        double sumReserveValue = 0;
        double sumValue = 0;
        static int cbHeight = 230;
        static int cbWidth = 675;
        static int cellJobSite = 185;
        static int cellName = 150;
        static int cellPosition = 150;
        static int cellClass = 150;
        IBLLHrmEmployee bllEmployee = new HrmEmployeeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            int loop = 0;
            if (ViewState["Loop"] != null)
            {
                loop = Convert.ToInt32(ViewState["Loop"]);
            }
            if (loop == 0)
            {
                btnCreateApprove_Click(null, null);
                btnCreateKnow_Click(null, null);
            }
            else
            {
                this.ApproveControlName = (List<string[]>)Session["ApproveControlName"];
                this.DelButtonApproveName = (List<string>)Session["DelButtonApproveName"];
                CreateApproveEmpControls(ref this.ApproveControlName, ref this.DelButtonApproveName);

                this.KnowControlName = (List<string[]>)Session["KnowControlName"];
                this.DelButtonKnowName = (List<string>)Session["DelButtonKnowName"];
                CreateKnowEmpControls(ref this.KnowControlName, ref this.DelButtonKnowName);
            }

            int times = 0;
            if (ViewState["Times"] != null)
            {
                times = Convert.ToInt32(ViewState["Times"]);
            }
            if (times == 0)
            {
                LoadGrid_Item();
            }
        }

        protected void LoadGrid_Item()
        {
            int seq = GetBidSeq();
            IList<PrePurchaseRequestItemInfo> infoPpritem = bllPprItem.GetBookMoneyForBid(seq);
            Grid_Item.DataSource = infoPpritem;
            Grid_Item.MasterTableView.GroupByExpressions.Clear();
            GridGroupByExpression geExpense = new GridGroupByExpression(Grid_Item.Columns[0]);
            GridGroupByExpression geDescr = new GridGroupByExpression(Grid_Item.Columns[1]);

            Grid_Item.MasterTableView.GroupByExpressions.Add(geExpense);
            Grid_Item.MasterTableView.GroupByExpressions.Add(geDescr);
            Grid_Item.DataBind();
            ViewState["Times"] = 1;
        }

        protected void Grid_Item_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadGrid_Item();
        }

        protected void Grid_Item_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                double price = 0;
                GridDataItem dataBoundItem = e.Item as GridDataItem;

                RadNumericTextBox txtPrice = ((RadNumericTextBox)(dataBoundItem["ReservePrice"].FindControl("txtPrice")));
                RadNumericTextBox txtQty = ((RadNumericTextBox)(dataBoundItem["ReserveQty"].FindControl("txtQty")));

                txtPrice.Attributes.Add("onFocus", "return FocusPrice('" + txtPrice.ClientID + "')");
                txtPrice.Attributes.Add("onBlur", "return BlurPrice('" + txtPrice.ClientID + "')");

                //txtQty.Attributes.Add("onFocus", "return Focus('" + txtPrice.ClientID + "','" + txtQty.ClientID + "')");
                //txtQty.Attributes.Add("onBlur", "return Blur('" + txtPrice.ClientID + "','" + txtQty.ClientID + "')");

                //if (((RadNumericTextBox)(dataBoundItem["ReserveQty"].FindControl("txtQty"))).Value.ToString().Length > 0)
                //{
                //    qty = double.Parse(((RadNumericTextBox)(dataBoundItem["ReserveQty"].FindControl("txtQty"))).Value.ToString());
                //}
                if (((RadNumericTextBox)(dataBoundItem["ReservePrice"].FindControl("txtPrice"))).Value.ToString().Length > 0)
                {
                    price = double.Parse(((RadNumericTextBox)(dataBoundItem["ReservePrice"].FindControl("txtPrice"))).Value.ToString());
                }

                sumValue += double.Parse(((Label)(dataBoundItem["SumAmount"].FindControl("lblSumAmount"))).Text);
                sumReserveValue += price;
            }
            else if (e.Item is GridFooterItem)
            {
                GridFooterItem footer = (GridFooterItem)e.Item;
                footer["InfoPpr.InfoJobSite.Name"].Controls.Add(new LiteralControl("<div align='right' style='color: Black; font-weight: bold;'>วงเงินจัดหาทั้งหมด :</div> "));
                ((Label)footer["SumAmount"].FindControl("lblAllSumAmount")).Text = String.Format("{0:N2}", sumValue);
                ((RadNumericTextBox)footer["ReservePrice"].FindControl("txtTotalPrice")).Value = Double.Parse(sumReserveValue.ToString());
            }
        }

        protected void btnCreateApprove_Click(object sender, EventArgs e)
        {
            int TmpNumOfName = ApproveControlName.Count;
            foreach (string[] cur in this.ApproveControlName)
            {
                if (cur[0] == "cbApproveEmployee" + TmpNumOfName.ToString())
                {
                    TmpNumOfName++;
                }
            }
            string[] ControlName = { "cbApproveEmployee" + TmpNumOfName.ToString(), "" };
            this.ApproveControlName.Add(ControlName);
            this.DelButtonApproveName.Add("delApproveEmployee" + TmpNumOfName.ToString());
            Session["ApproveControlName"] = this.ApproveControlName;
            Session["DelButtonApproveName"] = this.DelButtonApproveName;
            CreateApproveEmpControl(ref this.ApproveControlName, ref this.DelButtonApproveName);
            ViewState["Loop"] = 1;
        }

        protected void btnCreateKnow_Click(object sender, EventArgs e)
        {
            int TmpNumOfName = KnowControlName.Count;
            foreach (string[] cur in this.KnowControlName)
            {
                if (cur[0] == "cbKnowEmployee" + TmpNumOfName.ToString())
                {
                    TmpNumOfName++;
                }
            }
            string[] ControlName = { "cbKnowEmployee" + TmpNumOfName.ToString(), "" };
            this.KnowControlName.Add(ControlName);
            this.DelButtonKnowName.Add("delKnowEmployee" + TmpNumOfName.ToString());
            Session["KnowControlName"] = this.KnowControlName;
            Session["DelButtonKnowName"] = this.DelButtonKnowName;
            CreateKnowEmpControl(ref this.KnowControlName, ref this.DelButtonKnowName);
            ViewState["Loop"] = 1;
        }

        protected void btnDelApprove_Click(object sender, ImageClickEventArgs e)
        {
            Table TmpTB = (Table)RadPanelBar.FindItemByValue("BarH1").FindControl("tbApprove");
            ImageButton TmpIB = (ImageButton)sender;
            for (int i = 0; i < DelButtonApproveName.Count; i++)
            {
                if (DelButtonApproveName[i] == TmpIB.ID)
                {
                    TmpTB.Rows.RemoveAt(Convert.ToInt32(ApproveControlName[i][1]));
                    ApproveControlName.RemoveAt(i);
                    DelButtonApproveName.RemoveAt(i);
                    break;
                }
            }

            for (int i = 0; i < TmpTB.Rows.Count; i++)
            {
                foreach (Control child in TmpTB.Rows[i].Cells[0].Controls)
                {
                    if (child.GetType().ToString().Equals("System.Web.UI.WebControls.Label")
                              && child.ID.IndexOf("lbl_") == 0)
                    {
                        Label lbl = (Label)child;
                        lbl.Text = "ลำดับที่ " + i + " :";
                    }
                }
            }
        }

        protected void btnDelKnow_Click(object sender, ImageClickEventArgs e)
        {
            Table TmpTB = (Table)RadPanelBar.FindItemByValue("BarH2").FindControl("tbKnow");
            ImageButton TmpIB = (ImageButton)sender;
            for (int i = 0; i < DelButtonKnowName.Count; i++)
            {
                if (DelButtonKnowName[i] == TmpIB.ID)
                {
                    TmpTB.Rows.RemoveAt(Convert.ToInt32(KnowControlName[i][1]));
                    KnowControlName.RemoveAt(i);
                    DelButtonKnowName.RemoveAt(i);
                    break;
                }
            }

            for (int i = 0; i < TmpTB.Rows.Count; i++)
            {
                foreach (Control child in TmpTB.Rows[i].Cells[0].Controls)
                {
                    if (child.GetType().ToString().Equals("System.Web.UI.WebControls.Label")
                              && child.ID.IndexOf("lbl_") == 0)
                    {
                        Label lbl = (Label)child;
                        lbl.Text = "บุคคลที่ " + i + " :";
                    }
                }
            }
        }

        protected void CreateApproveEmpControls(ref List<string[]> ControlName, ref List<string> DelControlName)
        {
            if (ControlName.Count > 0)
            {
                for (int i = 0; i < ControlName.Count; ++i)
                {
                    TableRow tr = new TableRow();

                    TableCell tc1 = new TableCell();
                    TableCell tc2 = new TableCell();
                    TableCell tc3 = new TableCell();

                    Label lbl = new Label();
                    lbl.ID = "lbl_" + i.ToString();
                    lbl.Text = "ลำดับที่ " + (i + 1).ToString() + " : ";

                    RadComboBox TmpCB = new RadComboBox();

                    TmpCB.ID = ControlName[i][0].ToString();
                    TmpCB.EnableLoadOnDemand = true;
                    TmpCB.EmptyMessage = ".. พิมพ์ที่นี่เพื่อช่วยค้นหา .. ";
                    TmpCB.AllowCustomText = true;
                    TmpCB.Height = cbHeight;
                    TmpCB.HighlightTemplatedItems = true;
                    TmpCB.MarkFirstMatch = false;
                    TmpCB.ShowDropDownOnTextboxClick = true;
                    TmpCB.ShowMoreResultsBox = true;
                    TmpCB.EnableVirtualScrolling = true;
                    TmpCB.Width = cbWidth;
                    TmpCB.Style.Add("z-index", "8001");
                    HT h = new HT();
                    TmpCB.HeaderTemplate = h;
                    T t = new T();
                    TmpCB.ItemTemplate = t;
                    TmpCB.ItemsRequested += new RadComboBoxItemsRequestedEventHandler(Combobox_ItemsRequested);
                    TmpCB.OnClientTextChange = "onTextChangeGeneral";

                    ImageButton del = new ImageButton();
                    del.ID = DelControlName[i];
                    del.ImageUrl = "../Image/Grid/Cancel.gif";
                    del.Click += new ImageClickEventHandler(this.btnDelApprove_Click);

                    tc1.Controls.Add(lbl);
                    tc2.Controls.Add(TmpCB);
                    tc3.Controls.Add(del);

                    tr.Cells.AddAt(0, tc1);
                    tr.Cells.AddAt(1, tc2);
                    tr.Cells.AddAt(2, tc3);

                    Table TmpTB = (Table)RadPanelBar.FindItemByValue("BarH1").FindControl("tbApprove");

                    TmpTB.Rows.Add(tr);
                    ControlName[i][1] = (TmpTB.Rows.Count - 1).ToString();
                }
            }
        }

        protected void CreateApproveEmpControl(ref List<string[]> ControlName, ref List<string> DelControlName)
        {
            TableRow tr = new TableRow();

            TableCell tc1 = new TableCell();
            TableCell tc2 = new TableCell();
            TableCell tc3 = new TableCell();

            Label lbl = new Label();
            lbl.ID = "lbl_" + (ControlName.Count).ToString();
            lbl.Text = "ลำดับที่ " + (ControlName.Count).ToString() + " : ";

            RadComboBox TmpCB = new RadComboBox();

            TmpCB.ID = ControlName[ControlName.Count - 1][0].ToString();
            TmpCB.EnableLoadOnDemand = true;
            TmpCB.EmptyMessage = ".. พิมพ์ที่นี่เพื่อช่วยค้นหา .. ";
            TmpCB.AllowCustomText = true;
            TmpCB.Height = cbHeight;
            TmpCB.HighlightTemplatedItems = true;
            TmpCB.MarkFirstMatch = false;
            TmpCB.ShowDropDownOnTextboxClick = true;
            TmpCB.ShowMoreResultsBox = true;
            TmpCB.EnableVirtualScrolling = true;
            TmpCB.Width = cbWidth;
            TmpCB.Style.Add("z-index", "8001");
            HT h = new HT();
            TmpCB.HeaderTemplate = h;
            T t = new T();
            TmpCB.ItemTemplate = t;
            TmpCB.ItemsRequested += new RadComboBoxItemsRequestedEventHandler(Combobox_ItemsRequested);
            TmpCB.OnClientTextChange = "onTextChangeGeneral";

            ImageButton del = new ImageButton();
            del.ID = DelControlName[DelControlName.Count - 1];
            del.ImageUrl = "../Image/Grid/Cancel.gif";
            del.Click += new ImageClickEventHandler(this.btnDelApprove_Click);

            tc1.Controls.Add(lbl);
            tc2.Controls.Add(TmpCB);
            tc3.Controls.Add(del);

            tr.Cells.AddAt(0, tc1);
            tr.Cells.AddAt(1, tc2);
            tr.Cells.AddAt(2, tc3);

            Table TmpTB = (Table)RadPanelBar.FindItemByValue("BarH1").FindControl("tbApprove");

            TmpTB.Rows.Add(tr);
            ControlName[ControlName.Count - 1][1] = (TmpTB.Rows.Count - 1).ToString();
        }

        protected void CreateKnowEmpControls(ref List<string[]> ControlName, ref List<string> DelControlName)
        {
            if (ControlName.Count > 0)
            {
                for (int i = 0; i < ControlName.Count; ++i)
                {
                    TableRow tr = new TableRow();

                    TableCell tc1 = new TableCell();
                    TableCell tc2 = new TableCell();
                    TableCell tc3 = new TableCell();

                    Label lbl = new Label();
                    lbl.ID = "lbl_" + i.ToString();
                    lbl.Text = "บุคคลที่ " + (i + 1).ToString() + " : ";

                    RadComboBox TmpCB = new RadComboBox();

                    TmpCB.ID = ControlName[i][0].ToString();
                    TmpCB.EnableLoadOnDemand = true;
                    TmpCB.EmptyMessage = ".. พิมพ์ที่นี่เพื่อช่วยค้นหา .. ";
                    TmpCB.AllowCustomText = true;
                    TmpCB.Height = cbHeight;
                    TmpCB.HighlightTemplatedItems = true;
                    TmpCB.MarkFirstMatch = false;
                    TmpCB.ShowDropDownOnTextboxClick = true;
                    TmpCB.ShowMoreResultsBox = true;
                    TmpCB.EnableVirtualScrolling = true;
                    TmpCB.Width = cbWidth;
                    TmpCB.Style.Add("z-index", "8001");
                    HT h = new HT();
                    TmpCB.HeaderTemplate = h;
                    T t = new T();
                    TmpCB.ItemTemplate = t;
                    TmpCB.ItemsRequested += new RadComboBoxItemsRequestedEventHandler(Combobox_ItemsRequested);
                    TmpCB.OnClientTextChange = "onTextChangeGeneral";

                    ImageButton del = new ImageButton();
                    del.ID = DelControlName[i];
                    del.ImageUrl = "../Image/Grid/Cancel.gif";
                    del.Click += new ImageClickEventHandler(this.btnDelKnow_Click);

                    tc1.Controls.Add(lbl);
                    tc2.Controls.Add(TmpCB);
                    tc3.Controls.Add(del);

                    tr.Cells.AddAt(0, tc1);
                    tr.Cells.AddAt(1, tc2);
                    tr.Cells.AddAt(2, tc3);

                    Table TmpTB = (Table)RadPanelBar.FindItemByValue("BarH2").FindControl("tbKnow");

                    TmpTB.Rows.Add(tr);
                    ControlName[i][1] = (TmpTB.Rows.Count - 1).ToString();
                }
            }
        }

        protected void CreateKnowEmpControl(ref List<string[]> ControlName, ref List<string> DelControlName)
        {
            TableRow tr = new TableRow();

            TableCell tc1 = new TableCell();
            TableCell tc2 = new TableCell();
            TableCell tc3 = new TableCell();

            Label lbl = new Label();
            lbl.ID = "lbl_" + (ControlName.Count).ToString();
            lbl.Text = "บุคคลที่ " + (ControlName.Count).ToString() + " : ";

            RadComboBox TmpCB = new RadComboBox();

            TmpCB.ID = ControlName[ControlName.Count - 1][0].ToString();
            TmpCB.EnableLoadOnDemand = true;
            TmpCB.EmptyMessage = ".. พิมพ์ที่นี่เพื่อช่วยค้นหา .. ";
            TmpCB.AllowCustomText = true;
            TmpCB.Height = cbHeight;
            TmpCB.HighlightTemplatedItems = true;
            TmpCB.MarkFirstMatch = false;
            TmpCB.ShowDropDownOnTextboxClick = true;
            TmpCB.ShowMoreResultsBox = true;
            TmpCB.EnableVirtualScrolling = true;
            TmpCB.Width = cbWidth;
            TmpCB.Style.Add("z-index", "8001");
            HT h = new HT();
            TmpCB.HeaderTemplate = h;
            T t = new T();
            TmpCB.ItemTemplate = t;
            TmpCB.ItemsRequested += new RadComboBoxItemsRequestedEventHandler(Combobox_ItemsRequested);
            TmpCB.OnClientTextChange = "onTextChangeGeneral";

            ImageButton del = new ImageButton();
            del.ID = DelControlName[DelControlName.Count - 1];
            del.ImageUrl = "../Image/Grid/Cancel.gif";
            del.Click += new ImageClickEventHandler(this.btnDelKnow_Click);

            tc1.Controls.Add(lbl);
            tc2.Controls.Add(TmpCB);
            tc3.Controls.Add(del);

            tr.Cells.AddAt(0, tc1);
            tr.Cells.AddAt(1, tc2);
            tr.Cells.AddAt(2, tc3);

            Table TmpTB = (Table)RadPanelBar.FindItemByValue("BarH2").FindControl("tbKnow");

            TmpTB.Rows.Add(tr);
            ControlName[ControlName.Count - 1][1] = (TmpTB.Rows.Count - 1).ToString();
        }

        protected void Combobox_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            IList<HrmEmployeeInfo> infoList = bllEmployee.GetHrmEmployee(e.Text);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = (endOffset == infoList.Count);
            int j = 0;
            for (int i = itemOffset; i < endOffset; i++)
            {
                RadComboBox cbAppEmp = (RadComboBox)sender;
                cbAppEmp.Items.Add(new RadComboBoxItem(infoList[i].Name, infoList[i].Seq.ToString()));
                TableRow tr = new TableRow();
                TableCell tdJobSite = new TableCell();
                Label lblJobSite = new Label();
                lblJobSite.Text = infoList[i].InfoJobSite.Name;
                tdJobSite.Width = Unit.Pixel(cellJobSite);
                tdJobSite.Controls.Add(lblJobSite);
                tr.Cells.Add(tdJobSite);
                TableCell tdName = new TableCell();
                Label lblName = new Label();
                lblName.Text = infoList[i].Name;
                tdName.Width = Unit.Pixel(cellName);
                tdName.Controls.Add(lblName);
                tr.Cells.Add(tdName);
                TableCell tdPosition = new TableCell();
                Label lblPosition = new Label();
                lblPosition.Text = infoList[i].InfoPosition.Name;
                tdPosition.Width = Unit.Pixel(cellPosition);
                tdPosition.Controls.Add(lblPosition);
                tr.Cells.Add(tdPosition);
                TableCell tdClass = new TableCell();
                Label lblClass = new Label();
                lblClass.Text = infoList[i].InfoClass.Name;
                tdClass.Width = Unit.Pixel(cellClass);
                tdClass.Controls.Add(lblClass);
                tr.Cells.Add(tdClass);
                ((Table)cbAppEmp.Items[j].FindControl("tableInternalName")).Controls.Add(tr);
                j++;
            }

            e.Message = GetStatusMessage(endOffset, infoList.Count);
        }

        private static string GetStatusMessage(int offset, int total)
        {
            if (total <= 0)
                return "ไม่มีข้อมูล";

            return String.Format("รายการที่ <b>1</b>-<b>{0}</b> จากทั้งหมด <b>{1}</b> รายการ", offset, total);
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            BidBLL bllBid = new BidBLL();
            IList<PrePurchaseRequestItemInfo> infoListPprItem = new List<PrePurchaseRequestItemInfo>();

            foreach (GridDataItem data in Grid_Item.MasterTableView.Items)
            {
                PrePurchaseRequestItemInfo infoItem = new PrePurchaseRequestItemInfo();
                RadNumericTextBox txtPrice = ((RadNumericTextBox)(data["ReservePrice"].FindControl("txtPrice")));
                RadNumericTextBox txtQty = ((RadNumericTextBox)(data["ReserveQty"].FindControl("txtQty")));

                if (txtQty.Text == "")
                {
                    Session["Alert"] = "กรุณาระบุจำนวนซื้อให้ครบทุกรายการ";
                    Alert();
                    ReCalculateReserve();
                    return;
                }
                if (Convert.ToDouble(txtQty.Text) == 0)
                {
                    Session["Alert"] = "กรุณาระบุจำนวนซื้อที่ไม่ใช่ 0";
                    Alert();
                    ReCalculateReserve();
                    return;
                }
                if (txtPrice.Text == "")
                {
                    Session["Alert"] = "กรุณาระบุจำนวนเงินให้ครบทุกรายการ";
                    Alert();
                    ReCalculateReserve();
                    return;
                }
                if (Convert.ToDouble(txtPrice.Text) == 0)
                {
                    Session["Alert"] = "กรุณาระบุจำนวนเงินที่ไม่ใช่ 0";
                    Alert();
                    ReCalculateReserve();
                    return;
                }
                infoItem.Seq = Convert.ToInt32(data["Seq"].Text);
                infoItem.Qty = Convert.ToInt32(txtQty.Text);
                infoItem.Price = Convert.ToDouble(txtPrice.Text);
                infoListPprItem.Add(infoItem);
            }

            if (ApproveControlName.Count == 0)
            {
                Session["Alert"] = "กรุณาเพิ่มข้อมูลผู้อนุมัติ";
                Alert();
                ReCalculateReserve();
                return;
            }

            int cbSelectedValue = 0;
            for (int i = 0; i < ApproveControlName.Count; ++i)
            {
                RadComboBox TmpCB = (RadComboBox)RadPanelBar.FindItemByValue("BarH1").FindControl(this.ApproveControlName[i][0]);
                if (TmpCB.SelectedValue.ToString() != "")
                {
                    cbSelectedValue++;
                    break;
                }
            }

            if (cbSelectedValue == 0)
            {
                //Session["Alert"] = "กรุณาเลือกผู้อนุมัติอย่างน้อย 1 คน";
                //Alert();
                //ReCalculateReserve();
                //return;
            }

            List<BidRouteInfo> infoListRoute = new List<BidRouteInfo>();
            int Seq = GetBidSeq();
            BidInfo info = new BidInfo();
            HrmEmployeeInfo infoEmp = new HrmEmployeeInfo();
            info.Seq = Seq;
            info.DateConfirmed = DateTime.Now;
            infoEmp.Seq = 1;
            info.InfoEmployeeConfirmed = infoEmp;

            int No = 1;
            for (int i = 0; i < ApproveControlName.Count; ++i)
            {
                RadComboBox TmpCB = (RadComboBox)RadPanelBar.FindItemByValue("BarH1").FindControl(this.ApproveControlName[i][0]);
                if (TmpCB.SelectedValue.ToString() != "")
                {
                    BidRouteInfo infoRoute = new BidRouteInfo();
                    HrmEmployeeInfo infoFormEmp = new HrmEmployeeInfo();
                    HrmEmployeeInfo infoToEmp = new HrmEmployeeInfo();
                    RecordInfo infoRecord = new RecordInfo();
                    SysRouteStatusInfo infoRouteStstus = new SysRouteStatusInfo();

                    infoRoute.InfoBid = info;

                    infoFormEmp.Seq = 1;
                    infoRoute.InfoFromEmp = infoFormEmp;

                    infoToEmp.Seq = Convert.ToInt32(TmpCB.SelectedValue.ToString());
                    infoRoute.InfoToEmp = infoToEmp;

                    infoRoute.Order = No;
                    infoRoute.Type = "APPROVE";

                    infoRoute.Active = "Y";

                    infoRouteStstus.Status = "W";
                    infoRoute.InfoRouteStatus = infoRouteStstus;

                    infoRecord.Created_by = cn.UserID;
                    infoRecord.Created_when = DateTime.Now;
                    infoRoute.InfoRecord = infoRecord;

                    infoListRoute.Add(infoRoute);
                    No++;
                }
            }

            No = 1;
            for (int i = 0; i < KnowControlName.Count; ++i)
            {
                RadComboBox TmpCB = (RadComboBox)RadPanelBar.FindItemByValue("BarH2").FindControl(this.KnowControlName[i][0]);
                if (TmpCB.SelectedValue.ToString() != "")
                {
                    BidRouteInfo infoRoute = new BidRouteInfo();
                    HrmEmployeeInfo infoFormEmp = new HrmEmployeeInfo();
                    HrmEmployeeInfo infoToEmp = new HrmEmployeeInfo();
                    RecordInfo infoRecord = new RecordInfo();
                    SysRouteStatusInfo infoRouteStstus = new SysRouteStatusInfo();

                    infoRoute.InfoBid = info;

                    infoFormEmp.Seq = 1;
                    infoRoute.InfoFromEmp = infoFormEmp;

                    infoToEmp.Seq = Convert.ToInt32(TmpCB.SelectedValue.ToString());
                    infoRoute.InfoToEmp = infoToEmp;

                    infoRoute.Order = No;
                    infoRoute.Type = "INFORM";

                    infoRoute.Active = "Y";

                    infoRouteStstus.Status = "W";
                    infoRoute.InfoRouteStatus = infoRouteStstus;

                    infoRecord.Created_by = cn.UserID;
                    infoRecord.Created_when = DateTime.Now;
                    infoRoute.InfoRecord = infoRecord;

                    infoListRoute.Add(infoRoute);
                    No++;
                }
            }

            try
            {
                string TmpText = bllBid.UpdateTotalAndAmountConfirm(info, infoListPprItem, infoListRoute);
                if (TmpText == "1")
                {
                    RefreshParentDisplay();
                    Session["Alert"] = SystemInfo._ConfirmComplete;
                    Alert();
                    ReCalculateReserve();
                }
                else
                {
                    Session["Alert"] = TmpText;
                    Alert();
                    ReCalculateReserve();
                    return;
                }
            }
            catch (Exception ex)
            {
                Session["Alert"] = SystemInfo._ConfirmError;
                Alert();
                ReCalculateReserve();
                return;
            }
        }

        protected void ReCalculateReserve()
        {
            double price = 0;
            foreach (GridDataItem data in Grid_Item.MasterTableView.Items)
            {
                RadNumericTextBox txtPrice = ((RadNumericTextBox)(data["ReservePrice"].FindControl("txtPrice")));
                price += Convert.ToDouble(txtPrice.Text);
            }

            GridFooterItem footer = (GridFooterItem)Grid_Item.MasterTableView.GetItems(GridItemType.Footer)[0];
            ((RadNumericTextBox)footer["ReservePrice"].FindControl("txtTotalPrice")).Value = Double.Parse(price.ToString());
        }
    }
}